package com.cby.library.tool.util;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Point;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

/**
 * 屏幕相关信息工具类
 *
 * @author chenbaoyang
 */
public class ScreenUtils {
  /**
   * 描述：dip转换px
   * @param context
   * @param dipValue
   * @return
   */
  public static int dip2px(Context context, float dipValue){
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int)(dipValue * scale + 0.5f);
  }

  /**
   * 描述：px转换dip
   * @param context
   * @param pxValue
   * @return
   */
  public static int px2dip(Context context, float pxValue){
    final float scale = context.getResources().getDisplayMetrics().density;
    Log.i("TESTTTT","scale="+scale);
    return (int)(pxValue/scale + 0.5f);
  }

  /**
   * 从sp到px
   *
   * @param spValue
   * @return
   */
  public static int sp2px(Context context, float spValue) {
    final float fontScale =  context.getResources().getDisplayMetrics().scaledDensity;
    return (int) (spValue * fontScale + 0.5f);
  }

  /**
   * px 2 sp
   *
   * @param pxValue
   * @return
   */
  public static int px2sp(Context context, float pxValue) {
    final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
    return (int) (pxValue / fontScale + 0.5f);
  }

  /**
   * 获取屏幕高度
   * @param context
   * @return
   */
  public static int getScreenHeight(Context context){
    int screenHeight = 0;
    if (Build.VERSION.SDK_INT >= 13){
      Display display = getDefaultDisplay(context);
      Point size = new Point();
      display.getSize(size);
      screenHeight = size.y;
    }else{
      DisplayMetrics dm = context.getResources().getDisplayMetrics();

      screenHeight = dm.heightPixels;
    }

    return screenHeight;
  }

  /**
   * 获取屏幕宽度
   * @param context
   * @return
   */
  public static int getScreenWidth(Context context){
    int screenWidth = 0;
    if (Build.VERSION.SDK_INT >= 13){
      Display display = getDefaultDisplay(context);
      Point size = new Point();
      display.getSize(size);
      screenWidth = size.x;
    }else{
      DisplayMetrics dm = context.getResources().getDisplayMetrics();

      screenWidth = dm.widthPixels;
    }
    return screenWidth;
  }

  private static Display getDefaultDisplay(Context context){
    WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    return display;
  }


  /**
   * 返回当前屏幕是否为竖屏。
   * @param context
   * @return 当且仅当当前屏幕为竖屏时返回true,否则返回false。
   */
  public static boolean isScreenOriatationPortrait(Context context) {
    return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
  }
}
